﻿package mortal.game.scene3D.map3D
{
    import Message.Public.*;
    import com.fyGame.fyMap.*;
    import com.gengine.global.*;
    import flash.geom.*;
    import mortal.game.*;

    public class SceneRange extends Object
    {
        public static var screenResolutionX:Number = 1360;
        public static var screenResolutionY:Number = 768;
        private static var _entityRange:Rectangle = new Rectangle();
        private static var _effectRange:Rectangle = new Rectangle();
        private static const EffectPadding:int = 300;
        private static const EntityHeight:int = 200;
        private static const EntityWidth:int = 100;
        public static var loadMapRange:Rectangle = new Rectangle();
        public static var drawMapRange:Rectangle = new Rectangle();
        public static var map:Rectangle = new Rectangle();
        private static var _display:Rectangle = new Rectangle(0, 0, 1000, 580);
        private static var _scaleDisplay:Rectangle = new Rectangle(0, 0, 1000, 580);
        private static var _mapBg:Rectangle = new Rectangle();
        private static var _xFarPer:Number = 0.5;
        private static var _yFarPer:Number = 0.5;
        public static var bitmapFarXY:Rectangle = new Rectangle(0, 0, 1000, 580);
        private static var _sceneScale:Number = 1;
        public static var displayInt:Rectangle = new Rectangle(0, 0, 1000, 580);
        public static var loadMapFarRange:Rectangle = new Rectangle();
        public static var drawMapFarRange:Rectangle = new Rectangle();
        public static const loadExtendSize:int = 100;
        public static var moveMap:Rectangle = new Rectangle();
        public static var noMoveMap:Rectangle = new Rectangle(0, 0, 250, 100);
        private static var loadMapRec:Rectangle = new Rectangle();

        public function SceneRange()
        {
            return;
        }// end function

        public static function isSmallSceen() : Boolean
        {
            return screenResolutionX < 1400;
        }// end function

        public static function get scaleDisplay() : Rectangle
        {
            return _scaleDisplay;
        }// end function

        public static function get sceneScale() : Number
        {
            return _sceneScale;
        }// end function

        public static function set sceneScale(param1:Number) : void
        {
            _sceneScale = param1;
            updateScaleDisplay();
            return;
        }// end function

        public static function get display() : Rectangle
        {
            return _display;
        }// end function

        public static function getMousePoint() : SPoint
        {
            var _loc_1:* = new SPoint();
            _loc_1.x = Global.stage.mouseX + SceneRange.display.x;
            _loc_1.y = Global.stage.mouseY + SceneRange.display.y;
            return _loc_1;
        }// end function

        public static function set display(param1:Rectangle) : void
        {
            _display = param1;
            _entityRange.top = _display.top;
            _entityRange.left = _display.left - EntityWidth;
            _entityRange.bottom = _display.bottom + EntityHeight;
            _entityRange.right = _display.right + EntityWidth;
            _effectRange.top = _display.top - EffectPadding;
            _effectRange.left = _display.left - EffectPadding;
            _effectRange.bottom = _display.bottom + EffectPadding;
            _effectRange.right = _display.right + EffectPadding;
            bitmapFarXY.x = _xFarPer * _display.x;
            bitmapFarXY.y = _yFarPer * _display.y;
            displayInt.x = int(_display.x);
            displayInt.y = int(_display.y);
            displayInt.width = int(_display.width);
            displayInt.height = int(_display.height);
            updateScaleDisplay();
            updateNoMoveXY();
            return;
        }// end function

        private static function updateScaleDisplay() : void
        {
            var _loc_1:* = (_display.width / _sceneScale - _display.width) / 2;
            var _loc_2:* = (_display.height / _sceneScale - _display.height) / 2;
            _scaleDisplay.x = _display.x - _loc_1;
            _scaleDisplay.y = _display.y - _loc_2;
            _scaleDisplay.width = _display.width + 2 * _loc_1;
            _scaleDisplay.height = _display.height + 2 * _loc_2;
            if (_scaleDisplay.left < SceneRange.map.left)
            {
                _scaleDisplay.x = 0;
                displayInt.x = int(_loc_1);
            }
            if (_scaleDisplay.right > SceneRange.map.right)
            {
                _scaleDisplay.x = int(SceneRange.map.right - _scaleDisplay.width);
                displayInt.x = int(_scaleDisplay.x + _loc_1);
            }
            if (_scaleDisplay.top < SceneRange.map.top)
            {
                _scaleDisplay.y = 0;
                displayInt.y = int(_loc_2);
            }
            if (_scaleDisplay.bottom > SceneRange.map.bottom)
            {
                _scaleDisplay.y = int(SceneRange.map.bottom - _scaleDisplay.height);
                displayInt.y = int(_scaleDisplay.y + _loc_2);
            }
            updateBitmapAngle(_scaleDisplay, loadMapRange, drawMapRange, map);
            return;
        }// end function

        public static function updateBitmapAngle(param1:Rectangle, param2:Rectangle, param3:Rectangle, param4:Rectangle) : void
        {
            loadMapRec.left = param1.left - loadExtendSize;
            loadMapRec.right = param1.right + loadExtendSize;
            loadMapRec.top = param1.top - loadExtendSize;
            loadMapRec.bottom = param1.bottom + loadExtendSize;
            if (loadMapRec.left < 0)
            {
                loadMapRec.left = 0;
            }
            if (loadMapRec.right > param4.right)
            {
                loadMapRec.right = param4.right;
            }
            if (loadMapRec.top < 0)
            {
                loadMapRec.top = 0;
            }
            if (loadMapRec.bottom > param4.bottom)
            {
                loadMapRec.bottom = param4.bottom;
            }
            if (param1.left <= 0)
            {
                param1.left = 0;
                param2.left = 0;
                param3.left = 0;
            }
            else
            {
                param2.left = int(loadMapRec.left / MapConst.pieceWidth);
                param3.left = int(param1.left / MapConst.pieceWidth);
            }
            if (display.top <= 0)
            {
                param1.top = 0;
                param2.top = 0;
                param3.top = 0;
            }
            else
            {
                param2.top = int(loadMapRec.top / MapConst.pieceHeight);
                param3.top = int(param1.top / MapConst.pieceHeight);
            }
            param2.right = int((loadMapRec.right - 1) / MapConst.pieceWidth);
            param2.bottom = int((loadMapRec.bottom - 1) / MapConst.pieceHeight);
            param3.right = int((param1.right - 1) / MapConst.pieceWidth);
            param3.bottom = int((param1.bottom - 1) / MapConst.pieceHeight);
            return;
        }// end function

        public static function init(param1:FyMapInfo = null) : void
        {
            if (param1)
            {
                map.width = param1.gridWidth;
                map.height = param1.gridHeight;
                _mapBg.width = param1.bgMapWidth;
                _mapBg.height = param1.bgMapHeight;
                updateAngle();
                updateFarPer();
            }
            return;
        }// end function

        private static function updateFarPer() : void
        {
            _xFarPer = (_mapBg.width - _display.width) / (map.width - _display.width);
            _yFarPer = (_mapBg.height - _display.height) / (map.height - _display.height);
            return;
        }// end function

        public static function updateNoMoveXY() : void
        {
            return;
        }// end function

        private static function updateNoMoveWH() : void
        {
            noMoveMap.width = 0;
            noMoveMap.height = 0;
            return;
        }// end function

        public static function cancelNoMoveWH(param1:Boolean = false) : void
        {
            if (param1)
            {
                noMoveMap.width = 0;
                noMoveMap.height = 0;
            }
            else
            {
                noMoveMap.width = 250;
                noMoveMap.height = 100;
            }
            return;
        }// end function

        public static function updateAngle() : void
        {
            if (Game.mapInfo)
            {
                moveMap.width = Game.mapInfo.gridWidth - _display.width;
                moveMap.height = Game.mapInfo.gridHeight - _display.height;
                moveMap.x = int(_display.width * 0.5);
                moveMap.y = int(_display.height * 0.5);
                updateNoMoveWH();
                updateNoMoveXY();
            }
            return;
        }// end function

        public static function isInSceneByXY(param1:Number, param2:Number) : Boolean
        {
            return _entityRange.contains(param1, param2);
        }// end function

        public static function isInScene(param1:int, param2:int) : Boolean
        {
            return display.contains(param1, param2);
        }// end function

        public static function isInEffectRange(param1:int, param2:int) : Boolean
        {
            return _effectRange.contains(param1, param2);
        }// end function

        public static function isInEntityRange(param1:int, param2:int, param3:Number, param4:Number) : Boolean
        {
            if (display.left - param3 / 2 > param1 || display.right + param3 / 2 < param1 || display.top > param2 || display.bottom + param4 < param2)
            {
                return false;
            }
            return true;
        }// end function

        public static function resize() : void
        {
            return;
        }// end function

    }
}
